package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class
ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        ShoppingCart cartR = shoppingCartService.add(shoppingCart);
        return R.success(cartR);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(BaseContext.getCurrentId()!=null,ShoppingCart::getUserId,BaseContext.getCurrentId());
        qw.orderByDesc(ShoppingCart::getCreateTime);    //新添加的菜品置顶

        List<ShoppingCart> cartList = shoppingCartService.list(qw);
        return R.success(cartList);
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> cartClear() {

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(BaseContext.getCurrentId()!=null,ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(qw);

        return R.success("购物车清空成功!");
    }

    /**
     * 点减号从购物车删一个菜品或者一个套餐的方法
     * @return 删除成功或失败的消息
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        // 0.从前端发的请求可以看出是一个包含菜品id和套餐id的json对象
        // 1.创建查询构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        // 2.当操作菜品时，菜品id不为空
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            wrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart dishCart = shoppingCartService.getOne(wrapper);
            dishCart.setNumber(dishCart.getNumber()-1);
            Integer newNumber = dishCart.getNumber();
            if(newNumber>0){
                shoppingCartService.updateById(dishCart);
                return R.success(dishCart);
            }else if(newNumber == 0){
                shoppingCartService.removeById(dishCart.getId());
                return new R(1,"从购物车删除菜品成功",dishCart);
            }else {
                return R.error("操作异常");
            }
        }
        // 3.当操作套餐时，套餐id不为空
        Long setmealId = shoppingCart.getSetmealId();
        if(setmealId!=null){
            wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            wrapper.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart setmealCart = shoppingCartService.getOne(wrapper);
            setmealCart.setNumber(setmealCart.getNumber()-1);
            Integer newNumber = setmealCart.getNumber();
            if(newNumber>0){
                shoppingCartService.updateById(setmealCart);
                return R.success(setmealCart);
            }else if (newNumber == 0){
                shoppingCartService.removeById(setmealCart.getId());
                return new R(1,"从购物车删除套餐成功",setmealCart);
            }else{
                return R.error("操作异常");
            }
        }
        return R.error("操作异常");
    }

}
